﻿@using System.Data.SqlServerCe
@using Resources
@{
    Check.User(Can.CreateEmailForms);
    Validation.RequireFields("FormName", "Subject", "Legend", "SmtpServer", "ToAddress", "FromAddress");
    var fields = new[] { "ToAddress", "FromAddress" };
    Validation.Add(fields, Validator.Regex(@"^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$", "Must be a valid email address"));
    Validation.Add("Subject", Validator.StringLength(150));
    var yesno = new List<SelectListItem> {
        new SelectListItem { Value = "True", Text = "Yes" },
        new SelectListItem { Value = "False", Text = "No", Selected = true },
    };

    var formName = Request["FormName"];
    var legend = Request["Legend"];
    var smtpServer = Request["SmtpServer"];
    var port = Request["SmtpPort"];
    var smtpUserName = Request["SmtpUserName"];
    var smtpPassword = Request["SmtpPassword"];
    var fromAddress = Request["FromAddress"].OrDbNull();
    var smtpSsl = Request["SmtpSsl"];
    var subject = Request["Subject"];
    var toAddress = Request["ToAddress"];
    var beforeSendMessage = Request.Unvalidated("BeforeSendMessage");
    var sendMessage = Request.Unvalidated("SendMessage");
    var sendErrorMessage = Request.Unvalidated("SendErrorMessage");
    var showForm = Request["ShowForm"];

    if (IsPost && Validation.IsValid()) {
        var db = Database.Open((string)App.Database);
        var commandText = @"INSERT INTO Forms (FormName, Legend, SmtpServer, SmtpPort, SmtpUserName, SmtpPassword, FromAddress, 
                            SmtpEnableSsl, Subject, ToAddress, BeforeSendMessage, SendMessage, SendErrorMessage, ShowFormAfterSending)
                            VALUES (@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13)";
        try {
            db.Execute(commandText, formName, legend, smtpServer, port, smtpUserName, smtpPassword, fromAddress, smtpSsl, subject, toAddress, beforeSendMessage, sendMessage, sendErrorMessage, showForm);
            var formId = (int) db.GetLastInsertId();
            Response.Redirect("~/Admin/Widgets/FormWidgetFields/" + formId);
        }
        catch (SqlCeException ex) {
            if (ex.Message.StartsWith("A duplicate value")) {
                ModelState.AddError("FormName", "This is a duplicate of an existing form name. The form name must be unique.");
            }
        }
    }
}

<form method="post">
    <fieldset>
        <legend>Create Form</legend>
        @ControlGroup.TextBox("Form Name: ", "FormName")
        @ControlGroup.TextBox("Legend: ", "Legend", helperText: AdminResource.FormWidgetLegendHelpText)
        @ControlGroup.TextBox("Subject: ", "Subject", textBoxHtmlAttributes: new { style = "width:100%;" })
        @ControlGroup.TextBox("Email From Address: ", "FromAddress")
        @ControlGroup.TextBox("Email To Address: ", "ToAddress")
        @ControlGroup.TextArea("Message before sending:", "BeforeSendMessage", helperText: AdminResource.FormWidgetBeforeSendMessageHelpText, htmlAttributes: new { @class = "ckeditor" })
        @ControlGroup.TextArea("Message after sending:", "SendMessage", helperText: AdminResource.FormWidgetSendMessageHelpText, htmlAttributes: new { @class = "ckeditor" })
        @ControlGroup.TextArea("Message in event of sending error:", "SendErrorMessage", helperText: AdminResource.FormWidgetSendErrorMessageHelpText, htmlAttributes: new { @class = "ckeditor" })
        @ControlGroup.RadioButtonGroup("Show Form After Sending?", "ShowForm", yesno, inline: true, helperText: AdminResource.FormWidgetShowAfterSendingHelpText)
        @ControlGroup.TextBox("SMTP Server Address: ", "SmtpServer", helperText: AdminResource.FormWidgetSmtpServerHelpText)
        @ControlGroup.TextBox("SMTP Port: ", "SmtpPort")
        @ControlGroup.TextBox("SMTP User Name: ", "SmtpUserName")
        @ControlGroup.TextBox("SMTP Password: ", "SmtpPassword")
        @ControlGroup.RadioButtonGroup("Use SSL?", "SmtpSsl", yesno, inline: true)
        @ControlGroup.Button()
    </fieldset>
</form>

@section Script{
    <script type="text/javascript" src="/Scripts/ckeditor/ckeditor.js"></script>
}